package TeacherEvaluation.com.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.ibatis.reflection.wrapper.BaseWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import TeacherEvaluation.com.entity.ResultMsg;
import TeacherEvaluation.com.entity.Student;
import TeacherEvaluation.com.service.classService;
import TeacherEvaluation.com.service.studentService;


/**
 * 学生信息控制类
 * @author 陈龙
 *
 */
@Controller
public class studentController {
	//导入studentService bean
		@Autowired
		studentService studentService;
		@Autowired
		classService classService;
		
		//添加学生主页
		@RequestMapping("/stuAddIndex")
		public ModelAndView stuAddIndex() {
			ModelAndView view = new ModelAndView("admin/students/addStudents");
			view.addObject("items", classService.selectClassNum());
			return view;
		}

		//添加学生
		@RequestMapping("/stuAdd")
		@ResponseBody
		public ResultMsg departmentAdd(Student student) {
			System.out.println(student.getStu_number());
			student.setStu_password("123456");
			int i = studentService.insertStu(student);
			if (i==0) {
				return new ResultMsg(0, "学号已存在");
			}else {
				return new ResultMsg(1, "添加成功");
			}
		}
		
		
		//修改学生主页
		@RequestMapping("/stuUpdateIndex")
		public ModelAndView depUpdateIndex(String id) {
			System.out.println(id);
			ModelAndView view = new ModelAndView("admin/students/updateStudent");
			view.addObject("Student", studentService.selectStuByOne(id));
			view.addObject("items", classService.selectClassNum());
			return view;
		}
		
		
		
		//修改学生
		@RequestMapping("/stuUpdate")
		@ResponseBody
		public ResultMsg departmentUpdate(Student student) {
			int i = studentService.updateStu(student);
			//查询修改的院系名是否存在
			if (i==0) {
				return new ResultMsg(0, "修改失败");
			}else {
				return new ResultMsg(1, "修改成功");
			}
		}
		
		
		//学生个人信息
		@RequestMapping("/stuData")
		public ModelAndView stuData(HttpServletRequest request) {
			ModelAndView mv = new ModelAndView("students/myMessage");
			HttpSession session = request.getSession();
			Student student = (Student) session.getAttribute("user");
			mv.addObject("user", student);
			return mv;
		}
		//修改密码
		@RequestMapping("/updatePas")
		public ModelAndView updatePas() {
			ModelAndView mv = new ModelAndView("students/updatePas");
			return mv;
		}
		//修改学生密码
		@RequestMapping("/updatePwd")
		@ResponseBody
		public ResultMsg updatePwd(String stu_name2,HttpServletRequest request) {
			HttpSession session = request.getSession();
			Student student = (Student) session.getAttribute("user");
			String stu_number=student.getStu_number();
			int i = studentService.updatePas(stu_number,stu_name2);
			if(i==0) {
				
				return new ResultMsg(0, "修改失败");
			}else {
				return new ResultMsg(1, "修改成功");
			}
		}
		
		//删除院系
		@RequestMapping("/stuDelete")
		@ResponseBody
		public ResultMsg depDelete(String id) {
			int i = studentService.deleStu(Integer.parseInt(id));
			if (i==0) {
				return new ResultMsg(0,"删除失败");
			}else {
				return new ResultMsg(1,"删除成功");
			}
		}
		
		
		/*//查看学生
		@RequestMapping("/stuSelect1")
		@ResponseBody
		public ModelAndView departmentSelect() {
			//创建视图
			ModelAndView mView = new ModelAndView("admin/students/showStudents");
			//将查询结果发送给视图
			mView.addObject("itmes", studentService.selectStu());
			return mView;
		}*/
		
		
		/**
		 * 以下分页操作
		 */
		@RequestMapping("/stuSelect")
		@ResponseBody
		public ModelAndView  stuSelect(@RequestParam(value="currentPage",defaultValue="1",required=false)int currentPage){
			ModelAndView view = new ModelAndView("admin/students/showStudents");
			view.addObject("itmes", studentService.findByPage(currentPage));//回显分页数据
			return view;
		}

		
}
